﻿using BehaviorModel.Block;
using BehaviorModel.Block.Splitter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace BehaviorModel.XmlSerializer
{
    /// <summary>
    /// Class for Xml Serialization of Splitter Block
    /// </summary>
    class SplitterXmlSerializer : IXmlSerializer
    {        
        /// <summary>
        /// Serializes Splitter to XElement
        /// </summary>
        /// <returns></returns>
        public XElement ToXElement(IBlock block)
        {
            if (block is Splitter)
            {
                Splitter splitter = (Splitter)block;
                return new XElement(XmlSerializationConstants.SPLITTER_BLOCK,
                    new XElement(XmlSerializationConstants.DISCIPLINE, splitter.Discipline));
            }
            else
            {
                throw new ArgumentException();
            }
        }

        /// <summary>
        /// Deserializes XElement to Splitter
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        public IBlock FromXElement(XElement element)
        {
            Splitter splitter = new Splitter();
            splitter.Discipline = (SplitDiscipline)Enum.Parse(typeof(SplitDiscipline), element.Element(XmlSerializationConstants.SPLITTER_BLOCK).Element(XmlSerializationConstants.DISCIPLINE).Value);
            return splitter;
        }
    }
}
